#pragma once
#include "Acceptor.h"
#include "TcpConnection.h"
#include "Mutex.h"

#include <vector>
#include <map>
#include <memory>
#include <functional>
#include <sys/epoll.h>
using std::map;
using std::vector;
using std::shared_ptr;
using std::function;

using Functor = function<void()>;
class EventLoop
{
public:
    EventLoop(Acceptor & acceptor);
    ~EventLoop();
    void loop();
    void unloop();
    void setAllCallbacks(Callback &&Close, Callback &&Msg, Callback &&On);
    void runInLoop(Functor && cb);
private:
    void waitEpollFd();
    int createEpollFd();
    void AddEpollFd(int fd);
    void DelEpollFd(int fd);
    void handleNewConnection();
    void handleMessage(int fd);
    void doPendingFunctors();
    void wakeup();
    void handleRead();
    int createEventFd();



private:
    int _epfd;
    int _eventfd;
    vector<struct epoll_event> _eventList;
    bool _islooping;
    Acceptor & _acceptor;
    map<int, TcpConnectionPtr> _conns;
    vector<Functor> _pendingFunctors;
    Mutex _mux;
    Callback _onMessage;
    Callback _onClose;
    Callback _onConnection;

};

